package com.abc.smg;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

//中介类--代理类
public class Intermediary {
    private BuyHouse buyHouse;//买房人

    public Intermediary(BuyHouse buyHouse) {
        this.buyHouse = buyHouse;
    }
    //生成代理对象
    public Object getProxy(){
        ILogger iLogger = new ILogger();//切面类==做日志
        BuyHouse buyHouse = new BuyHouseImpl();
        Object proxy= Proxy.newProxyInstance(Intermediary.class.getClassLoader(),
                this.buyHouse.getClass().getInterfaces(), new InvocationHandler() {
                    @Override
                    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                        iLogger.before();//开始日志记录
                        Object invoke = method.invoke(buyHouse, args);
                        iLogger.after();//结束日志记录
                        return invoke;
                    }
                });
        return proxy;
    }
}
